<?php

namespace App\Http\Controllers\Admin;

use App\Model\Article;
use App\Model\Cate;
use http\Message;
use Illuminate\Support\Facades\Storage;
use Markdown;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Image;
use App\Services\OSS;

class ArticleController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $arts = Article::paginate(10);
//        $listkey = 'LIST:ARTICLE';
//        $hashkey = 'HASH:ARTICLE';
//        $arts = [];
//        if(Redis::exists($listkey)){
//            //存放所有要获取的文章ID
//            $lists = Redis::lrange($listkey,'0','-1');
//            foreach ($lists as $k => $v){
//                $arts[] = Redis::hgetall($hashkey.$v);
//            }
//            Redis::expire($listkey,'10');
//        }else{
//            //连接数据库 ，获取需要的数据
//            $arts = Article::get()->toArray();
//
//            //将数据存入reids
//            foreach($arts as $k=>$v){
//                //将文章的ID存入listkey
//                Redis::rpush($listkey,$v['art_id']);
//                Redis::hmset($hashkey.$v['art_id'],$v);
//            }
//        }
        return view('admin.article.list',compact('arts'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $cates = (new Cate)->tree();
        return view('admin.article.add',compact('cates'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $input = $request->except('photo','_token');
        $input['art_time'] = time();
        $res = Article::insertGetId($input);

        if($res){
            $data = [
                'status'=>0,
                'message'=>'成功！'
            ];
        }else{
            $data = [
                'status'=>1,
                'message'=>'失败！'
            ];
        }
        return $data;

    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $arts = Article::find($id);
        $cates = Cate::get();
        return view('admin.article.edit',compact('arts','cates'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $input = $request->except('_token','photo');
        $res = Article::where('art_id',$input['art_id'])->update($input);

        if($res){
            $data = [
                'status'=>0,
                'message'=>'成功！'
            ];
        }else{
            $data = [
                'status'=>1,
                'message'=>'失败！'
            ];
        }
        return $data;
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $res = Article::where('art_id',$id)->delete();

        //删除缓存中的

        if($res){
            $data = [
                'status'=>0,
                'message'=>'删除成功！'
            ];
        }else{
            $data = [
                'status'=>1,
                'message'=>'删除失败！'
            ];
        }
        return $data;
    }
    
    //文件上传
    public function upload(Request $request)
    {
        $file = $request->file('photo');
        //判断上传文件是否成功
        if(!$file->isValid()){
            return response()->json(['ServerNo'=>'400','ResultData'=>'无效的上传文件']);
        }
        //获取文件拓展名
        $ext = $file -> getClientOriginalExtension();

        //生成新文件名
        $newName = md5(time().rand(10000,99999)).'.'.$ext;

        //指定上传路径
        $path = public_path('uploads/');

        //2将文件上传到OSS仓库
        //$newName 文件上传到oss的新文件名
        //$path 眼上传的文件资源
        $res = OSS::upload($newName,$file->getRealPath());
//        //将文件移动到指定位置
////        $res= Image::make($file)->resize(100,100)->save($path.'/'.$newName);
////        //判断移动文件是否成功
///
        //3.将文件上传到七牛云存储
//        $res = \Storage::disk('qiniu')->writeStream($newName,fopen($file->getRealPath(),'r'));

        if(!$res){
            return response()->json(['ServerNo'=>'400','ResultData'=>'保存文件失败']);

        }

        return response()->json(['ServerNo'=>'200','ResultData'=>$newName]);
    }

    //返回markdown语法
    public function pre_mk(Request $request)
    {
        $input = $request->all();
        return Markdown::convertToHtml($input['cont']);
    }


    //添加或取消推荐位

    public function recommend(Request $request)
    {
        $input = $request->all();

        //将redis缓存中的status记录同时修改
//        $redis = Redis::hget('HASH:ARTICLE'.$input['id'],'art_status');
//        if($redis == 0){
//            Redis::hset('HASH:ARTICLE'.$input['id'],'art_status','1');
//        }else{
//            Redis::hset('HASH:ARTICLE'.$input['id'],'art_status','0');
//    }
        $art = Article::find($input['id']);
        if($art->art_status == 0){
            $art->art_status == 1;
        }else{
            $art->art_status == 0;
        }
        $res = $art->save();


        if($res){
            $data = [
                'status'=>0,
                'message'=>'成功！'
            ];
        }else{
            $data = [
                'status'=>1,
                'message'=>'失败！'
            ];
        }
        return $data;

    }
}
